package advanced.network.basic_servlet;

import java.io.*;
import java.net.Socket;
import java.util.Date;

import static advanced.network.demo.Utils.release;

/**
 * 封装响应信息
 * 1. 内容可以动态添加
 * 2. 关注状态码, 拼接号对应的协议信息
 */
public class Response {
    private static final String CRTF = System.getProperty("line.separator");
    private BufferedWriter bw;
    private StringBuilder headInfo;
    private StringBuilder content;
    private int contentLen = 0;//正文的字节数


    private Response() {
        content = new StringBuilder();
        headInfo = new StringBuilder();
    }

    public Response(OutputStream os) {
        this();
        bw = new BufferedWriter(new OutputStreamWriter(os));
    }

    public Response(Socket client) {
        this();
        try {
            bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //动态添加内容(流打印!!!) 返回对象自身
    private Response print(String info) {
        content.append(info);
        contentLen += info.getBytes().length;
        return this;
    }

    private Response println(String info) {
        content.append(info);
        contentLen += (info + CRTF).getBytes().length;
        return this;
    }

    /**
     * 创建协议头信息
     *
     * @param code
     */
    private void createHeadInfo(int code) {
        headInfo = new StringBuilder();
        String blank = " ";
        //1. 响应行: http/1.1 200 ok
        headInfo.append("HTTP/1.1").append(blank);
        headInfo.append(code).append(blank);
        switch (code) {
            case 404:
                headInfo.append("NOT FOUND").append(CRTF);
                break;
            case 505:
                headInfo.append("SERVER ERROR").append(CRTF);
                break;
            default:
                headInfo.append("OK").append(CRTF);
                break;
        }

        //2. 响应头:
        headInfo.append("Date:").append(new Date()).append(CRTF);
        headInfo.append("Server:").append("test Server/0.0.1;charset=UTF-8").append(CRTF);
        headInfo.append("Content-Type: text/html:").append(CRTF);
        headInfo.append("Content-Length:").append(contentLen).append(CRTF);
        headInfo.append(CRTF);
    }

    /**
     * 写出响应信息到浏览器
     *
     * @param code
     */
    public void push2Brower(int code, String msg) {

        //3. 正文(返回的内容)
        makeContent(msg);
        createHeadInfo(code);//创建头信息
        //4. 写出到客户端

        try {
            bw.append(headInfo);
            bw.append(content);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private Response makeContent(String responseContent) {
        this.print("<html>");
        this.print("<head>");
        this.print("<title>");
        this.print("服务器响应成功");
        this.print("</title>");
        this.print("</head>");
        this.print("<body>");
        this.print(responseContent);
        this.print("</body>");
        this.print("</html>");
        return this;
    }

}
